package cn.gov.pbc.audit.excel;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import cn.gov.pbc.audit.common.LoanConst;
import cn.gov.pbc.audit.pojo.Loan;
import cn.gov.pbc.audit.util.DateTimeUtil;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class LoanExcelListener extends AnalysisEventListener<LoanExcelRow> {
	
	
	
	private Integer rowIndex = 2;
	
	private List<Loan> loanList;
	
	private Integer institutionId;
	
	private Integer clientType;
	
	public LoanExcelListener() {
		this.loanList = new ArrayList<>(60000);
	}
	
	

	@Override
	public void invoke(LoanExcelRow data, AnalysisContext context) {
		if(rowIndex % 10000 == 0) {
			System.out.println("正在处理第" + this.rowIndex + "行");
		}
		if(data.getId() == null && StringUtils.isBlank(data.getClientName())) {
			System.out.println("第" + this.rowIndex + "行数据为空");
			return;
		}
		this.loanList.add(this.assmbleLoan(data));
		this.rowIndex++;
	}
	
	private Loan assmbleLoan(LoanExcelRow row) {
		Loan loan = new Loan();
		loan.setInstitutionId(this.institutionId);
		loan.setOrgName(row.getOrgName());
		loan.setOrgCode(row.getOrgCode());
		loan.setClientCode(row.getClientCode());
		loan.setClientName(row.getClientName());
		loan.setClientNo(row.getClientNo());
		loan.setClientType(this.clientType);
		loan.setClientId(row.getClientId());
		BigDecimal loanNumber;
		try {
			loanNumber = new BigDecimal(row.getLoan()).setScale(4, RoundingMode.HALF_UP);
			loan.setLoan(loanNumber);
		} catch (Exception e) {
			throw new RuntimeException("第" + this.rowIndex + "行，贷款发放金额格式错误");
		}
		BigDecimal loanBalance;
		try {
			loanBalance = new BigDecimal(row.getLoanBalance()).setScale(4, RoundingMode.HALF_UP);
			loan.setLoanBalance(loanBalance);
		} catch (Exception e) {
			throw new RuntimeException("第" + this.rowIndex + "行，贷款余额格式错误");
		}
		BigDecimal interestRate;
		try {
			interestRate = new BigDecimal(row.getInterestRate()).setScale(4, RoundingMode.HALF_UP);
			loan.setInterestRate(interestRate);
		} catch (Exception e) {
			throw new RuntimeException("第" + this.rowIndex + "行，贷款利率格式错误");
		}
		Date loanStartDay = DateTimeUtil.strToDate(row.getLoanStartDay(), LoanConst.STANDARD_DATE_FORMAT);
		if(loanStartDay == null) {
			loanStartDay = DateTimeUtil.strToDate(row.getLoanStartDay(), LoanConst.DASH_DATE_FORMAT);
			if(loanStartDay == null) {
				loanStartDay = DateTimeUtil.strToDate(row.getLoanStartDay(), LoanConst.SLASH_DATE_FORMAT);
				if(loanStartDay == null) {
					throw new RuntimeException("第" + this.rowIndex + "行， 贷款发放日格式错误");	
				}
			}
		}
		loan.setLoanStartDay(loanStartDay);
		Date loanEndDay = DateTimeUtil.strToDate(row.getLoanEndDay(), LoanConst.STANDARD_DATE_FORMAT);
		if(loanEndDay == null) {
			loanEndDay = DateTimeUtil.strToDate(row.getLoanEndDay(), LoanConst.DASH_DATE_FORMAT);
			if(loanEndDay == null) {
				loanEndDay = DateTimeUtil.strToDate(row.getLoanEndDay(), LoanConst.SLASH_DATE_FORMAT);
				if(loanEndDay == null) {
					throw new RuntimeException("第" + this.rowIndex + "行， 贷款到期日格式错误");
				}
			}
		}
		loan.setLoanEndDay(loanEndDay);
		loan.setLoanType(row.getLoanType());
		String loanTypeDetail = row.getLoanTypeDetail();
		if(this.clientType == 0 && StringUtils.isNotBlank(loanTypeDetail)) {
			if(!loanTypeDetail.equals("普惠口径")) {
				throw new RuntimeException("第" + this.rowIndex + "行，贷款划型细项填写错误");
			}
		}
		loan.setLoanTypeDetail(row.getLoanTypeDetail());
		LoanConst.LoanStatus loanStatus = LoanConst.LoanStatus.searchByValue(StringUtils.trim(row.getLoanStatus()));
		if(loanStatus == null) {
			throw new RuntimeException("第" + this.rowIndex + "行， 贷款质量格式错误");
		}
		loan.setLoanStatus(loanStatus.getCode());
		loan.setLoanUse(row.getLoanUse());
		loan.setGuaranteeType(row.getGuaranteeType());
		loan.setBail(row.getBail());
		loan.setPledgeType(row.getPledgeType());
		loan.setExternalCreditEnhancement(row.getExternalCreditEnhancement());
		loan.setNote(row.getNote());
		return loan;
	}



	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		System.out.println("共读取" + (this.rowIndex - 2) + "行数据");
	}

}
